<template>
  <div id="app">
    <el-container class="app-container">
      <!-- 左侧导航栏 -->
      <el-aside :width="isCollapsed ? '64px' : '240px'" class="sidebar" :class="{ 'is-collapsed': isCollapsed }">
        <div class="logo-container">
          <transition name="fade">
            <div v-if="!isCollapsed">
              <h2 class="logo-title">Dify知识库</h2>
              <p class="logo-subtitle">管理系统</p>
            </div>
            <div v-else class="logo-icon">
              <el-icon :size="28"><FolderOpened /></el-icon>
            </div>
          </transition>
        </div>
        
        <el-menu
          :default-active="activeMenu"
          router
          class="sidebar-menu"
          background-color="#001529"
          text-color="#ffffff"
          active-text-color="#1890ff"
          :collapse="isCollapsed"
        >
          <el-menu-item index="/qa">
            <el-icon><ChatDotRound /></el-icon>
            <template #title>智能问答</template>
          </el-menu-item>
          <el-menu-item index="/history">
            <el-icon><Clock /></el-icon>
            <template #title>问题记录</template>
          </el-menu-item>
          <el-menu-item index="/unanswered">
            <el-icon><QuestionFilled /></el-icon>
            <template #title>未解答问题</template>
          </el-menu-item>
          <el-menu-item index="/sop">
            <el-icon><Document /></el-icon>
            <template #title>SOP生成</template>
          </el-menu-item>
          <el-menu-item index="/documents">
            <el-icon><FolderOpened /></el-icon>
            <template #title>知识库管理</template>
          </el-menu-item>
        </el-menu>

        <!-- 折叠按钮 -->
        <div class="collapse-button" @click="toggleCollapse">
          <el-icon :size="18">
            <DArrowLeft v-if="!isCollapsed" />
            <DArrowRight v-else />
          </el-icon>
        </div>
      </el-aside>

      <!-- 右侧内容区域 -->
      <el-container class="main-container">
        <el-header class="top-header">
          <div class="header-content">
            <div class="breadcrumb">
              <span class="page-title">{{ pageTitle }}</span>
            </div>
            
            <div class="user-selector">
              <el-select 
                v-model="currentUserId" 
                @change="handleUserChange"
                placeholder="选择用户"
                size="default"
                class="user-select"
              >
                <el-option
                  v-for="user in users"
                  :key="user.id"
                  :label="`${user.username} (${user.role})`"
                  :value="user.id"
                >
                  <span>{{ user.username }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">
                    {{ user.role }}
                  </span>
                </el-option>
              </el-select>
              <div class="current-user-info">
                <el-icon class="user-icon"><User /></el-icon>
                <span>{{ currentUser.username }}</span>
                <el-tag size="small" type="info" class="role-tag">{{ currentUser.role }}</el-tag>
              </div>
            </div>
          </div>
        </el-header>
        
        <el-main class="content-main">
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { useRoute } from 'vue-router'
import { userStore } from './store/user'
import { ElMessage } from 'element-plus'
import { 
  ChatDotRound, 
  Clock, 
  QuestionFilled, 
  Document, 
  FolderOpened,
  User,
  DArrowLeft,
  DArrowRight
} from '@element-plus/icons-vue'

const route = useRoute()
const activeMenu = computed(() => route.path)
const isCollapsed = ref(false)

const currentUserId = ref(userStore.currentUser.id)
const currentUser = computed(() => userStore.currentUser)
const users = userStore.users

const pageTitle = computed(() => {
  const titles = {
    '/qa': '智能问答',
    '/history': '问题记录',
    '/unanswered': '未解答问题',
    '/sop': 'SOP生成',
    '/documents': '知识库管理'
  }
  return titles[route.path] || 'Dify知识库管理系统'
})

const toggleCollapse = () => {
  isCollapsed.value = !isCollapsed.value
}

const handleUserChange = (userId) => {
  const user = users.find(u => u.id === userId)
  if (user) {
    userStore.setCurrentUser(user)
    ElMessage.success(`已切换到用户: ${user.username}`)
  }
}
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

#app {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif, 'Microsoft YaHei', '微软雅黑';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  height: 100vh;
  overflow: hidden;
}

.app-container {
  height: 100vh;
}

/* 左侧边栏样式 */
.sidebar {
  background-color: #001529;
  height: 100vh;
  overflow-y: auto;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.15);
  transition: width 0.3s ease;
  position: relative;
}

.logo-container {
  padding: 24px 20px;
  text-align: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  background: linear-gradient(135deg, #001529 0%, #002140 100%);
  min-height: 88px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.logo-icon {
  color: #ffffff;
}

.logo-title {
  color: #ffffff;
  font-size: 20px;
  font-weight: 600;
  margin: 0;
  letter-spacing: 1px;
}

.logo-subtitle {
  color: rgba(255, 255, 255, 0.65);
  font-size: 12px;
  margin: 4px 0 0 0;
}

.sidebar-menu {
  border-right: none;
  padding: 10px 0;
}

.sidebar-menu .el-menu-item {
  height: 48px;
  line-height: 48px;
  margin: 4px 12px;
  border-radius: 6px;
  transition: all 0.3s;
}

.sidebar.is-collapsed .sidebar-menu .el-menu-item {
  margin: 4px 8px;
  padding: 0 !important;
  justify-content: center;
}

.sidebar-menu .el-menu-item:hover {
  background-color: rgba(255, 255, 255, 0.08) !important;
}

.sidebar-menu .el-menu-item.is-active {
  background-color: #1890ff !important;
  color: #ffffff !important;
}

.sidebar-menu .el-menu-item .el-icon {
  font-size: 18px;
  margin-right: 8px;
}

.sidebar.is-collapsed .sidebar-menu .el-menu-item .el-icon {
  margin-right: 0;
}

/* 折叠按钮 */
.collapse-button {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  width: 40px;
  height: 40px;
  background-color: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s;
  color: #ffffff;
}

.collapse-button:hover {
  background-color: rgba(255, 255, 255, 0.2);
  transform: translateX(-50%) scale(1.1);
}

/* 淡入淡出动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 主内容区域 */
.main-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #f0f2f5;
}

.top-header {
  background-color: #ffffff;
  padding: 0 24px;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  height: 64px !important;
  display: flex;
  align-items: center;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.breadcrumb {
  flex: 1;
}

.page-title {
  font-size: 18px;
  font-weight: 500;
  color: #262626;
}

.user-selector {
  display: flex;
  align-items: center;
  gap: 16px;
}

.user-select {
  width: 180px;
}

.current-user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background-color: #f5f5f5;
  border-radius: 6px;
  font-size: 14px;
  color: #262626;
}

.user-icon {
  font-size: 16px;
  color: #1890ff;
}

.role-tag {
  margin-left: 4px;
}

.content-main {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
  background-color: #f0f2f5;
}

/* 滚动条样式 */
.sidebar::-webkit-scrollbar,
.content-main::-webkit-scrollbar {
  width: 6px;
}

.sidebar::-webkit-scrollbar-thumb {
  background-color: rgba(255, 255, 255, 0.2);
  border-radius: 3px;
}

.content-main::-webkit-scrollbar-thumb {
  background-color: rgba(0, 0, 0, 0.2);
  border-radius: 3px;
}

.sidebar::-webkit-scrollbar-track,
.content-main::-webkit-scrollbar-track {
  background-color: transparent;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    width: 200px !important;
  }
  
  .logo-title {
    font-size: 16px;
  }
  
  .current-user-info {
    display: none;
  }
}
</style>
